package exam.controller;

import com.github.pagehelper.PageInfo;
import exam.bean.Student;
import exam.service.IStudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/students")
public class StudentController {

    @Resource
    private IStudentService studentService;

    @RequestMapping("")
    String home() {
        return "Hello World!";
    }

    @RequestMapping(value = "/student", method = {RequestMethod.POST, RequestMethod.GET})
    public String queryStudents(@RequestParam(name = "pageNum", required = false, defaultValue = "1") int pageNum,
                                @RequestParam(name = "pageSize", required = false, defaultValue = "10") int pageSize,
                                @RequestParam Map<String, Object> queryMap, Model model, HttpServletRequest request) {

        HttpSession session = request.getSession();
        session.setAttribute("username", "测试");

        List<Student> studentList = studentService.selectStudent(pageNum, pageSize);

        model.addAttribute("pageInfo", new PageInfo<>(studentList));
        model.addAttribute("studentList", studentList);
        return "index";
    }

    // 获取学生列表
    @ResponseBody
    @GetMapping("/list")
    public List<Student> list() {
        return studentService.selectStudent(1, 10);
    }


    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(Model model) {
        model.addAttribute("name", "测试");
        return "hello";
    }

    @ResponseBody
    @PostMapping("/add")
    public boolean add(Student student) {

        return studentService.insertStu(student) > 0 ? true : false;


    }

    @GetMapping("/toAdd")
    public String toAdd() {

        return "add";
    }

    @GetMapping("/toUpdate")
    public String toUpdate(int id, Model model) {
        //查出来之前用户填写的信息是什么
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        //复用了 查询详情的方法  当然没问题
        Student student = studentService.selectStudentSingle(map);

        model.addAttribute("student", student);

        return "update";
    }

    @ResponseBody
    @PostMapping("/update")
    public boolean update(Student student) {

        return studentService.updateStudent(student) > 0 ? true : false;

    }

    @ResponseBody
    @GetMapping("/delete")
    public boolean delete(int id) {
        return studentService.deleteStudent(id) > 0 ? true : false;
    }

    @GetMapping("/toDetail")
    public String toDetail(int id, Model model) {

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);

        Student student = studentService.selectStudentSingle(map);

        model.addAttribute("student", student);

        return "detail";
    }


}